﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using DataAccess;
using DevExpress.XtraEditors;
using DevExpress.XtraPrinting.Native;

namespace QuanLyKhachSan
{
    public partial class FormTimHoaDon : XtraForm,ITimKiem<HoaDonDichVu>
    {
        public FormTimHoaDon()
        {
            InitializeComponent();
        }

        private void FormTimHoaDon_Load(object sender, EventArgs e)
        {
            cbbLoaiTim.SelectedIndex = 0;
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            Close();
        }

        public Func<HoaDonDichVu, bool> Filter { get; set; }

        public IEnumerable<HoaDonDichVu> Result { get; set; }

        private void btnTim_Click(object sender, EventArgs e)
        {
            var findType = TimTheo();
            String content = txtNoidungtim.Text;
            IEnumerable<Exception> errors = KiemTraGiaTriHopLe(findType, content);
            var exceptions = errors as Exception[] ?? errors.ToArray();
            if (!exceptions.Any())
            {
                Func<HoaDonDichVu, bool> newFilter;
                switch (findType)
                {
                    case TimHoaDonTheo.TimTheoTenKhachHang:
                        newFilter = dv => dv.KhachHang.HoTenKH.Contains(content);
                        break;
                    case TimHoaDonTheo.TimTheoNgayLap:
                        newFilter = dv => dv.NgaySuDung.Equals(DateTime.Parse(content));
                        break;
                    default:
                        newFilter = dv => dv.NhanVien.MaNV.Equals(int.Parse(content));
                        break;
                }
                var list = new HoaDonDichVuManager().GetListOrderServices().Result.Where(newFilter);
                var hoaDonDichVus = list as HoaDonDichVu[] ?? list.ToArray();
                if (!hoaDonDichVus.Any())
                {
                    XtraMessageBox.Show("Không tìm thấy");
                }
                else
                {
                    Result = hoaDonDichVus.ToList();
                    DialogResult = DialogResult.OK;
                }
            }
            else
            {
                //hien thi loi nhap sai noi dung
                lblInfo.Text = exceptions.ToList()[0].ToString();
            }
        }

        private TimHoaDonTheo TimTheo()
        {
            String findType = cbbLoaiTim.Text;
            switch (findType)
            {
                    
                case "Tìm theo tên khách hàng":
                    return TimHoaDonTheo.TimTheoTenKhachHang;
                case "Tìm theo ngày thành lập":
                    return TimHoaDonTheo.TimTheoNgayLap;
                default:
                    return TimHoaDonTheo.TimTheoMaNVLap;
            }
        }

        private IEnumerable<Exception> KiemTraGiaTriHopLe(TimHoaDonTheo loaiTim, String value)
        {
            var exceptions = new List<Exception>();
            switch (loaiTim)
            {
                case TimHoaDonTheo.TimTheoTenKhachHang:
                   if(txtNoidungtim.Text.IsEmpty())
                       exceptions.Add(new Exception("Vui lòng nhập mã khách hàng"));
                    break;
                case TimHoaDonTheo.TimTheoNgayLap:
                    try
                    {
                        DateTime.Parse(value);
                    }
                    catch (Exception )
                    {
                        exceptions.Add(new Exception("Ngày lập: yyyy/MM/dd"));
                    } break;
                default:
                    try
                    {
                        int.Parse(value);
                    }
                    catch (Exception )
                    {
                        exceptions.Add(new Exception("Mã nhân viên phải là kiểu số nguyên"));
                    }
                    break;
            }
            return exceptions;
        }

        private void txtNoidungtim_TextChanged(object sender, EventArgs e)
        {
            lblInfo.Text = "";
        }

        private void txtNoidungtim_TextChanged_1(object sender, EventArgs e)
        {
            if (lblInfo.Text.Any())
                lblInfo.Text = "";
        }


    }
}
